<?php

//include 'C:\xampp\htdocs\semuaberbagi.com\database\koneksi\Koneksi.php';
include '../database/koneksi/Koneksi.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Barang
 *
 * @author BAGUS
 */
class Barang {

    //put your code here
    var $id_Barang;
    var $nama_Barang;
    var $deskripsi;
    var $tgl_Unggah;
    var $tgl_Expired;
    var $view;
    var $status;
    var $id_Kategori;
    var $id_Provinsi;
    var $id_Pengguna;

    public function getId_Barang() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $result = mysql_query("select * from barang where id_kategori = '"
                . mysql_real_escape_string($this->getId_Kategori()) . "' and id_pengguna = '"
                . mysql_real_escape_string($this->getId_Pengguna()) . "' order by id_barang");
        $id_Barang1 = '';

        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }

        while ($row = mysql_fetch_array($result)) {
            $id_Barang1 = $row['id_barang'];
        }
        if ($id_Barang1 == '') {
            $id_Barang1 = $this->getId_Kategori() . $this->getId_Pengguna() . '101';
            $this->id_Barang = $id_Barang1;
        } else {
            $id_kategori1 = substr($id_Barang1, 0, -3);
            $id_kategoridanpengguna = $this->getId_Kategori() . $this->getId_Pengguna();
            if ($id_kategoridanpengguna == $id_kategori1) {
                $id_Barangr = intval($id_Barang1) + 1;
                $this->id_Barang = intval($id_Barangr);
            } else {
                $this->id_Barang = $this->getId_Kategori() . $this->getId_Pengguna() . '101';
            }
        }
        return $this->id_Barang;
    }

    public function setNama_Barang($inNama_Barang) {
        $this->nama_Barang = $inNama_Barang;
    }

    public function getNama_Barang() {
        return $this->nama_Barang;
    }

    public function setDeskripsi($inDeskripsi) {
        $this->deskripsi = $inDeskripsi;
    }

    public function getDeskripsi() {
        return $this->deskripsi;
    }

    public function getTgl_Unggah() {
        $this->tgl_Unggah = date("Y-m-d H:i:s");
        return $this->tgl_Unggah;
    }

    public function getTgl_Expired() {
        $this->tgl_Expired = date('Y-m-d H:i:s', strtotime('+90 day', strtotime(date('Y-m-d H:i:s'))));
        return $this->tgl_Expired;
    }

    public function setStatus($inStatus) {
        $this->status1 = $inStatus;
    }

    public function getStatus() {
        return $this->status1;
    }

    public function setId_Kategori($inId_Kategori) {
        $this->id_Kategori = $inId_Kategori;
    }

    public function getId_Kategori() {
        return $this->id_Kategori;
    }
    
    public function setId_Provinsi($inId_Provinsi) {
        $this->id_Provinsi = $inId_Provinsi;
    }

    public function getId_Provinsi() {
        return $this->id_Provinsi;
    }

    public function setId_Pengguna($inId_Pengguna) {
        $this->id_Pengguna = $inId_Pengguna;
    }

    public function getId_Pengguna() {
        return $this->id_Pengguna;
    }

    public function insert_Barang() {
        $koneksi = new Koneksi();
        $koneksi->koneksiDB();
        $query = mysql_query("insert into barang values('" . mysql_real_escape_string($this->getId_Barang()) . "','"
                . mysql_real_escape_string($this->getNama_Barang()) . "','"
                . mysql_real_escape_string($this->getDeskripsi()) . "','"
                . $this->getTgl_Unggah() . "','"
                . $this->getTgl_Expired() . "','"
                . mysql_real_escape_string($this->getStatus()) . "','"
                . mysql_real_escape_string($this->getId_Kategori()) . "','"
                . mysql_real_escape_string($this->getId_Provinsi()) . "','"
                . mysql_real_escape_string($this->getId_Pengguna()) . "')");
        if (!$query) {
            return 'error : ' . mysql_error();
        } else {
            return 'sukses';
        }
    }
}
